package com.example.take_out_dish.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.take_out_dish.common.Result;
import com.example.take_out_dish.dto.SetmealDishDto;
import com.example.take_out_dish.dto.SetmealDto;
import com.example.take_out_dish.entity.Setmeal;
import com.example.take_out_dish.service.SetmealDishService;
import com.example.take_out_dish.service.SetmealService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 套餐 前端控制器
 * </p>
 *
 * @author yyyyys
 * @since 2024-07-01
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Resource
    private SetmealService setmealService;

@GetMapping("/page")
public Result<Page<SetmealDto>> page(
        @RequestParam(defaultValue = "1") int page,
        @RequestParam(defaultValue = "10") int pageSize,
        @RequestParam(required = false) String name,
        @RequestParam(required = false) String categoryId,
        @RequestParam(required = false) Integer status) {
    Page<SetmealDto> dishDTOPage = setmealService.pageWithCategoryName(page, pageSize, name, categoryId, status);
    return Result.success(dishDTOPage);
}

    @PostMapping("/saveDish")
    public Result<Object> saveSetmeal(@RequestBody Setmeal setmeal){
        setmealService.saveSetmeal(setmeal);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result<Object> getId(@PathVariable String id){
        Setmeal SetmealbyId = setmealService.getById(id);
        return Result.success(SetmealbyId);
    }
    @PostMapping("/status")
    public Result<Object> updateStatus(@RequestParam("id") String id){
        Setmeal setmeal = setmealService.getById(id);
        if (setmeal.getStatus()==0){
            setmeal.setStatus(1);
        }else {
            setmeal.setStatus(0);
        }
        setmealService.updateById(setmeal);
        return Result.success();
    }
    @DeleteMapping()
    public Result<Object> deleteById(@RequestParam("ids") String id){
        log.info("id:{}",id);
        Setmeal setmeal = setmealService.getById(id);
        setmeal.setIsDeleted(1);
        setmealService.updateById(setmeal);
        return Result.success();
    }
    @PostMapping
    public Result<Object> addSetmeal(@RequestBody SetmealDishDto setmealDishDto) {
        setmealService.addSetmealDishDto(setmealDishDto);
        return Result.success();
    }
    @PutMapping()
    public Result<Object> updateSetmeal(@RequestBody SetmealDishDto setmealDishDto) {
        setmealService.updateSetmealDishDto(setmealDishDto);
        return Result.success();
    }

}
